package com.xuan.collegeblog.spider.restapi;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.moxi.cloudblog.base.enums.EPublish;
import com.moxi.cloudblog.base.enums.EStatus;
import com.moxi.cloudblog.base.exception.ThrowableUtils;
import com.moxi.cloudblog.base.global.Constants;
import com.moxi.cloudblog.base.global.ECode;
import com.moxi.cloudblog.base.holder.RequestHolder;
import com.moxi.mogublog.commons.entity.xuan.College;
import com.moxi.mogublog.utils.IpUtils;
import com.moxi.mogublog.utils.ResultUtil;
import com.moxi.mogublog.utils.StringUtils;
import com.moxi.mogublog.xo.global.MessageConf;
import com.moxi.mogublog.xo.global.RedisConf;
import com.moxi.mogublog.xo.global.SysConf;
import com.moxi.mogublog.xo.service.xuan.CollegeService;
import com.moxi.mogublog.xo.service.xuan.CollegeSortService;
import com.moxi.mogublog.commons.vo.college.CollegeSortVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.concurrent.TimeUnit;

@Api(value = "大学博客相关接口", tags = {"大学博客相关接口"})
@RestController
@RequestMapping("/college")
@Slf4j
public class CollegeRestApi {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private CollegeSortService collegeSortService;

    @Autowired
    private CollegeService collegeService;

    @ApiOperation(value = "获取大学列表", notes = "获取大学列表", response = String.class)
    @PostMapping("/getList")
    public String getCollegeList(@RequestBody CollegeSortVO collegeSortVO) {
        // System.out.println("#############################" + collegeSortVO.toString());
        log.info("#############################" + collegeSortVO.toString());
        return ResultUtil.result(SysConf.SUCCESS, collegeSortService.getCollegePageList(collegeSortVO));
    }


    // @BussinessLog(value = "点击分类", behavior = EBehavior.VISIT_CLASSIFY)       //要研究一下这个的作用是什么
    @ApiOperation(value = "通过collegeSortUid获取文章", notes = "通过collegeSortUid获取文章")
    @GetMapping("/getArticleByCollegeSortUid")
    public String getArticleByCollegeSortUid(HttpServletRequest request,
                                             @ApiParam(name = "collegeSortUid", value = "分类UID", required = false) @RequestParam(name = "collegeSortUid", required = false) String collegeSortUid,
                                             @ApiParam(name = "currentPage", value = "当前页数", required = false) @RequestParam(name = "currentPage", required = false, defaultValue = "1") Long currentPage,
                                             @ApiParam(name = "pageSize", value = "每页显示数目", required = false) @RequestParam(name = "pageSize", required = false, defaultValue = "10") Long pageSize) {
        log.info("############################# getArticleByCollegeSortUid");
        if (StringUtils.isEmpty(collegeSortUid)) {
            log.info("点击分类,传入collegeSortUid不能为空");
            return ResultUtil.result(SysConf.ERROR, "传入collegeSortUid不能为空");
        }
        log.info("通过collegeSortUid获取文章列表");
        return ResultUtil.result(SysConf.SUCCESS, collegeService.getListByCollegeSortUid(collegeSortUid, currentPage, pageSize));
    }


//    @ApiOperation(value = "新增college", notes = "新增college")
//    @PostMapping("/addCollege")
//    public String addCollege(@RequestBody CollegeVo collegeVo) {
//        return ResultUtil.result(SysConf.SUCCESS, collegeService.addCollege(collegeVo));
//    }


    // @BussinessLog(value = "点击校园博客", behavior = EBehavior.BLOG_CONTNET)
    @ApiOperation(value = "通过Uid获取校园博客内容", notes = "通过Uid获取校园博客内容")
    @GetMapping("/getBlogByUid")
    public String getBlogByUid(@ApiParam(name = "uid", value = "博客UID", required = false) @RequestParam(name = "uid", required = false) String uid,
                               @ApiParam(name = "oid", value = "博客OID", required = false) @RequestParam(name = "oid", required = false, defaultValue = "0") Integer oid,
                               @ApiParam(name = "ip", value = "博客ip", required = false) @RequestParam(name = "ip", required = false, defaultValue = "0") String uip) {

        log.info("############################# getBlogByUid");
        HttpServletRequest request = RequestHolder.getRequest();
        if (StringUtils.isEmpty(uid) && oid == 0) {
            return ResultUtil.result(SysConf.ERROR, MessageConf.PARAM_INCORRECT);
        }
        College college = null;
        if (StringUtils.isNotEmpty(uid)) {
            college = collegeService.getById(uid);
        } else {
            QueryWrapper<College> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq(SysConf.OID, oid);
            queryWrapper.last(SysConf.LIMIT_ONE);
            college = collegeService.getOne(queryWrapper);
        }

        if (college == null || college.getStatus() == EStatus.DISABLED || EPublish.NO_PUBLISH.equals(college.getIsPublish())) {
            return ResultUtil.result(ECode.ERROR, MessageConf.BLOG_IS_DELETE);
        }

        // 设置文章版权申明
        // setBlogCopyright(blog);

        //设置博客标签
        // blogService.setTagByBlog(blog);

        //获取分类
        // blogService.setSortByBlog(blog);

        //设置博客标题图
        // setPhotoListByBlog(blog);

        String ip = IpUtils.getIpAddr(request);
        if (!StringUtils.isEmpty(uip)) {
            ip = uip;
        }
        if (!StringUtils.isEmpty(ip)) {
            //从Redis取出数据，判断该用户是否点击过
            String jsonResult = stringRedisTemplate.opsForValue().get("BLOG_CLICK:" + ip + "#" + college.getUid());

            if (StringUtils.isEmpty(jsonResult)) {

                //给博客点击数增加
                Integer clickCount = college.getClickCount() + 1;
                college.setClickCount(clickCount);
                college.updateById();

                //将该用户点击记录存储到redis中, 24小时后过期
                stringRedisTemplate.opsForValue().set(RedisConf.BLOG_CLICK + Constants.SYMBOL_COLON + ip + Constants.SYMBOL_WELL + college.getUid(), college.getClickCount().toString(),
                        24, TimeUnit.HOURS);
            }
        }

        return ResultUtil.result(SysConf.SUCCESS, college);
    }

    @ApiOperation(value = "增加college点击数", notes = "增加college点击数")
    @GetMapping("/addClick")
    public String addClick(@ApiParam(name = "cid", value = "校园CID", required = true)
                           @RequestParam(name = "cid", required = true) String cid) {
        log.info("############################# addClick");
        return ResultUtil.result(SysConf.SUCCESS, collegeSortService.addClickCount(cid));
    }

    @ApiOperation(value = "获取大学Hot列表", notes = "获取大学Hot列表", response = String.class)
    @GetMapping("/getHotList")
    public String getHotCollegeList() {
        log.info("############################# getHot");
        return ResultUtil.result(SysConf.SUCCESS, collegeSortService.getHotCollegeSortList());
    }

    @ApiOperation(value = "获取Hot大学Topic", notes = "获取Hot大学Topic", response = String.class)
    @GetMapping("/getHotTopic")
    public String getHotTopic() {
        log.info("############################# getHotTopic");
        return ResultUtil.result(SysConf.SUCCESS, collegeService.getHotCollegeList());
    }

}
